{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 环境准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sh\n",
    "\n",
    "conda remove -n ipex-llm-cpu --all -y\n",
    "conda create -n ipex-llm-cpu python=3.11 libuv -y\n",
    "conda activate ipex-llm-cpu\n",
    "\n",
    "pip install --pre --upgrade numpy==1.26.4 torch==2.1.2+cpu torchvision==0.16.2+cpu torchaudio==2.1.2+cpu --index-url https://download.pytorch.org/whl/cpu\n",
    "pip install --pre --upgrade ipex-llm --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/\n",
    "pip install modelscope==1.22.3 openai==1.55.3 httpx==0.27.2 addict==2.4.0 simplejson==3.19.3 python-dateutil==2.9.0.post0 sortedcontainers==2.4.0\n",
    "pip install typer==0.15.1 fastapi==0.115.7 uvicorn==0.34.0 sse-starlette==2.2.1 omegaconf==2.3.0 datasets==3.2.0 soundfile==0.13.1\n",
    "pip install transformers==4.37.0 accelerate==0.31.0 sentencepiece==0.2.0 diffusers==0.29.2 librosa==0.10.2.post1\n",
    "pip install vector_quantize_pytorch==1.12.17 vocos==0.1.0 cn2an==0.5.22 pypinyin==0.50.0 jieba==0.42.1 g2p_en==2.1.0 txtsplit==1.0.0 cached_path==1.6.3 num2words==0.5.12 anyascii==0.3.2 jamo==0.4.1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CPU 使用案例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 图片生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url=\"http://localhost:8010/image/stable_diffusion_xl/images/generations\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({'prompt': 'a dog, 8K'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "from IPython.display import Image\n",
    "Image(response.json()[\"data\"][\"b64_json\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语音生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url=\"http://localhost:8010/tts/melotts/generate_audio_file\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8', 'accept': 'application/json'}\n",
    "data=json.dumps({'input': '开发 AI 应用从此简单，汇聚最新最热 AI 模型，提供模型体验、推理、训练、部署和应用的一站式服务，提供充沛算力，做中国最好的 AI 社区'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "from IPython.display import Audio\n",
    "Audio(response.content, rate=24000)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ipex-llm-cpu",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
